package com.wind.gtest.GMapItemManager;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.wind.gtest.GTApplication;
import com.wind.gtest.R;

public class GMapGroundManager {
	private static GMapGroundManager instance = null;
	Bitmap bit;
	static float ratelat = 0;
	static float ratelong = 0;

	GMapGroundManager() {
		// TODO Auto-generated constructor stub
	}

	public static GMapGroundManager getInstance() {
		if (instance == null) {
			instance = new GMapGroundManager();
		}
		return instance;
	}

	public GroundOverlayOptions getGroundOptSample() {
		// 26.095542, 119.3093684);
		// cover
		// LatLng latback = new LatLng(0, 0);
		// LatLng latback1 = new LatLng(latback.latitude - 180,
		// latback.longitude - 90);
		// LatLng latback2 = new LatLng(latback.latitude + 180,
		// latback.longitude + 90);
		// LatLngBounds boundsback = new LatLngBounds(latback1, latback2);
		// BitmapDescriptor imageback = BitmapDescriptorFactory
		// .fromResource(R.drawable.white);
		// // BitmapFactory.decodeResource(
		// // getResources(), );
		// GroundOverlay groundOverlayback = map
		// .addGroundOverlay(new GroundOverlayOptions().image(imageback)
		// .positionFromBounds(boundsback));
		// cover
		ratelat+=0.001;
		ratelong+=0.001;
		LatLng lat = new LatLng(26.095, 119.3093);
		LatLng lat1 = new LatLng(lat.latitude - 0.009+ratelat, lat.longitude - 0.01+ratelong);
		LatLng lat2 = new LatLng(lat.latitude + 0.009+ratelat, lat.longitude + 0.01+ratelong);
		LatLngBounds bounds = new LatLngBounds(lat1, lat2);
		// BitmapFactory.Options opt = new Options();
		// opt.inSampleSize = 1;

		// Bitmap bit = BitmapFactory.decodeResource(GTApplication.getInstance()
		// .getResources(), R.drawable.big, opt);
//		if (!bit.isRecycled()) {
//			bit.recycle();
//		}
		bit = readBitMap(GTApplication.getInstance(), R.drawable.big);
		BitmapDescriptor image = BitmapDescriptorFactory.fromBitmap(bit);

		// BitmapDescriptorFactory
		// // .fromResource(R.drawable.img869);
		// .fromResource(R.drawable.img869);
		return new GroundOverlayOptions().image(image).positionFromBounds(
				bounds);
	}

	/**
	 * 以最省記憶體的方式讀取本地資源的圖片
	 * 
	 * @param context
	 * @param resId
	 * @return
	 */
	public static Bitmap readBitMap(Context context, int resId) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		opt.inSampleSize = 2;
		// 獲取資源圖片
		InputStream is = context.getResources().openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}
}
